
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentPage: 1,
    pageLimit: 10,
    orderList: [],
    total_page: 0,
    dataHidden: '1',
    lastTime: 0, //最后保存时间
    screeningCol: true,
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight
        })
      }
    })
    util.showLoading("加载中...");
  },

  onShow:function(){
    this.setData({
      currentPage: 1,
      total_page: 0
    })
    this.dateTime();
    this.loadOrderRecord();
  },

  //初始时间设置
  dateTime: function () {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    month = (month >= 10) ? month : '0' + month;
    day = (day >= 10) ? day : '0' + day;
    var formatDate = year + '-' + month + '-' + day;
    this.setData({
      endDate: formatDate,
      startDate: "请选择开始日期",
      ofEndDate: formatDate,
    })
  },

  //订单列表
  loadOrderRecord: function () {
    var that = this;
    var params = new Object();
    var a = /^(\d{4})-(\d{2})-(\d{2})$/
    if (a.test(this.data.start_time)) {
      params.start_time = this.data.startDate;
    }
    if (a.test(this.data.endDate)) {
      params.end_time = this.data.endDate;
    }
    params.page = that.data.currentPage;
    params.limit = that.data.pageLimit;
    api.POST({
      params: params,
      url: app.globalData.base_url + 'shop/orderList',
      success: function (res) {
        wx.stopPullDownRefresh();
        util.hiddenLoading();
        if (res.data.code == '') {
          var currentPage = res.data.data.search.page;
          var total_page = res.data.data.search.total_page;
          if (currentPage == 1) {
            var orderList = [];
          } else {
            var orderList = that.data.orderList;
          }
          orderList = orderList.concat(res.data.data.order_list);
          that.setData({
            total_page: total_page,
            orderList: orderList,
            dataHidden: (orderList.length > 0) ? 1 : 2
          })

        } else {
          util.showError(res.data.message)
        }
      },
      fail: function (fail) {
        wx.stopPullDownRefresh();
        util.hiddenLoading();
        util.showError("网络错误，请检查网络。");
      }
    })
  },

  // 查看详情
  orderDetailMethod: function (e) {
    var curTime = e.timeStamp;
    var lastTime = this.data.lastTime;
    this.setData({
      lastTime: curTime
    })
    if (lastTime > 0) {
      if (curTime - lastTime < 1500) {
        return false;//禁止连续点击
      }
    }
    var oid = e.currentTarget.dataset.oid;
    wx.navigateTo({
      url: '../orderDetails/orderDetails?oid=' + oid,
      // url: '../orderVerification/orderVerification?oid=' + oid,
    })
  },

  //加载更多
  onReachBottom: function () {
    var currentPage = this.data.currentPage;
    var total_page = this.data.total_page;
    if (currentPage < total_page) {
      this.setData({
        currentPage: currentPage + 1
      })
      this.loadOrderRecord();
    }
  },

  //刷新
  onPullDownRefresh: function () {
    this.refreshData();
  },
  //刷新数据
  refreshData: function () {
    var that = this;
    that.setData({
      currentPage: 1
    })
    that.dateTime();
    setTimeout(function () {
      that.loadOrderRecord();
    }, 1000)
  },

  //显示/隐藏日期选择组件
  screeningSel: function () {
    this.setData({
      screeningCol: !this.data.screeningCol
    })
  },

  //开始时间
  startDateChange: function (e) {
    this.setData({
      startDate: e.detail.value
    })
  },

  //结束时间
  endDateChange: function (e) {
    this.setData({
      endDate: e.detail.value
    })
  },

  //时间筛选
  confirm: function () {
    var a = /^(\d{4})-(\d{2})-(\d{2})$/
    if (a.test(this.data.startDate)) {
      var start_time = this.data.startDate;
    }
    if (a.test(this.data.endDate)) {
      var end_time = this.data.endDate;
    }
    this.setData({
      currentPage: 1,
      start_time: start_time,
      end_time: end_time,
      screeningCol: true
    })
    this.loadOrderRecord();
  },

  //隐藏筛选时间组件
  cancel: function () {
    this.setData({
      screeningCol: true
    })
  },

  //取消提货
  cancelClaimGoods:function(e){
    var that = this;
    var curTime = e.timeStamp;
    var lastTime = this.data.lastTime;
    this.setData({ lastTime: curTime})
    if ((lastTime > 0) && (curTime - lastTime < 1500)) {
        return false;//禁止连续点击
    }
    var oid = e.currentTarget.dataset.oid;
    wx.showModal({
      title: '提示',
      content: '确定取消提货',
      success: function (res) {
        if (res.confirm) {
          api.POST({
            params: { oid: oid },
            url: app.globalData.base_url + 'shop/cancelPickUp',
            success: function (res) {
              if (util.isEmpty(res.data.code)) {
                util.showError("取消提货成功");
                that.onShow();
              } else {
                util.showError(res.data.message);
              }
            },
            fail: function (fail) {
              util.showError("网络错误，请检查网络。");
            }
          })
        }
      }
    })

  },

  //扫码开柜
  scanCodeMethod: function (e) {
    var that = this;
    var curTime = e.timeStamp;
    var lastTime = this.data.lastTime;
    this.setData({
      lastTime: curTime
    })
    if (lastTime > 0) {
      if (curTime - lastTime < 1500) {
        return false;//禁止连续点击
      }
    }
    var oid = e.currentTarget.dataset.oid;
    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {
        var rest = res.result;
        if (rest.indexOf("gzjShopId?machineid") < 0) {
          util.showStatus("请扫描机器二维码");
          return;
        }
        var arr = res.result.split("?");
        var str1 = arr[1];
        var atrr1 = str1.split("&")[0];
        var machine_id = atrr1.split("=")[1];
        // var shopId = str1.split("&")[1].split("=")[1];
        wx.navigateTo({
          url: '../openDoor/openDoor?machine_id=' + machine_id + '&oid=' + oid,
        })
      }
    })
  },

  // 订单支付
  pay: function (e) {
    var that = this;
    var curTime = e.timeStamp;
    var lastTime = this.data.lastTime;
    this.setData({
      lastTime: curTime
    })
    if (lastTime > 0) {
      if (curTime - lastTime < 1500) {
        return false;//禁止连续点击
      }
    }
    var index = e.currentTarget.dataset.index;
    var orderList = that.data.orderList;
    var orderInfo = orderList[index];
    var oid = orderInfo.oid;
    api.POST({
      params: { oid: oid },
      url: app.globalData.base_url + 'payment/getWechatInfo',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          var preData = res.data.data.payinfo;
          wx.requestPayment({
            timeStamp: preData.timeStamp,
            nonceStr: preData.nonceStr,
            package: preData.package,
            signType: preData.signType,
            paySign: preData.paySign,
            success: function (res) {
              that.refreshData();
            },
            fail: function (error) {
              util.showError("支付失败");
            }
          })
        } else {
          util.showError(res.data.message);
        }
      },
      fail: function (fail) {
        util.showError("网络错误，请检查网络。");
      }
    })
  },

  //开发票
  openInvoice:function(e){
    var oid = e.currentTarget.dataset.oid;
    wx.navigateTo({
      url: '../invoice/invoice?oid=' + oid,
    })
  }
})